﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using CrusadeGmTool.Diagnostics;
using System.Xml.Serialization;
using CrusadeGmTool.Models;
using System.Xml;
using CrusadeGmTool.Net;

namespace CrusadeGmTool.Forms
{
    public partial class ServerList : Form
    {
      
        Logger _log;
        TreeNode _beingAdded;
        TreeNode _Current;
        EncryptedSerializer<List<LoginInfo>> _data;

        public ServerList()
        {
            _log = new Logger("ServerList");
            InitializeComponent();

       /*     EncryptedSerializer<LoginInfo> toto = new EncryptedSerializer<LoginInfo>("test", "test.dat");
            toto.ToSerialize.Object = new LoginInfo() { Servername = "TOTOKIROX" };
            toto.Encrypt();
            LoginInfo test = toto.Decrypt();*/

           _data = new EncryptedSerializer<List<LoginInfo>>("qwertyuiopasdfgh", "servers.dat");
            
            TreeNode MainNode = createTreeNodes();
            
            tv_serverlist.Nodes.Add(MainNode);
            tv_serverlist.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(tv_serverlist_NodeMouseDoubleClick);
        }

        void tv_serverlist_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag == null)
                this.CreateNewNode();
        }

        void CreateNewNode()
        {

        }

        private TreeNode createTreeNodes()
        {
            TreeNode main = new TreeNode("Liste des serveurs");
            List<LoginInfo> servers = getServerList();

            foreach (LoginInfo server in servers)
            {
                TreeNode serverNode = new TreeNode(server.Servername);
                serverNode.Tag = server;
                main.Nodes.Add(serverNode);
            }

            return main;
         }

        private List<LoginInfo> getServerList()
        {
            _log.Info("Decrypting server list...");

            List<LoginInfo> servers = null;
            try
            {
                servers = _data.Decrypt();
            }
            catch (Exception e)
            {
                return null;
            }
            return servers;
        }

        private void ServerList_Load(object sender, EventArgs e)
        {

        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            _beingAdded = new TreeNode("Nouveau serveur");
            this.tv_serverlist.Nodes[0].Nodes.Add(_beingAdded);
            tv_serverlist.Nodes[0].Expand();
            tv_serverlist.SelectedNode = _beingAdded;
            
            tb_adress.Enabled = true;
            tb_name.Enabled = true;
            tb_password.Enabled = true;
            tb_username.Enabled = true;
            nm_port.Enabled = true;
            btn_save.Enabled = true;
            tb_name.Focus();
        }

        private void EncryptTreeViewContent()
        {
            List<LoginInfo> servers = new List<LoginInfo>();
            foreach(TreeNode node in tv_serverlist.Nodes[0].Nodes)
            {
                if (node.Tag != null)
                {
                    servers.Add((LoginInfo)(node.Tag));
                }
            }
            _data.ToSerialize.Object = servers;
            _data.Encrypt();
            servers = null;
            servers = _data.Decrypt();
        }

        private void tb_name_KeyUp(object sender, KeyEventArgs e)
        {
            _beingAdded.Text = tb_name.Text;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            LoginInfo info = new LoginInfo()
            {
                HostAdress = tb_adress.Text,
                Password = tb_password.Text,
                Servername = tb_name.Text,
                Username = tb_username.Text,
                Port = (int)(nm_port.Value)
            };
            _beingAdded.Tag = info;
            EncryptTreeViewContent();
        }
    }
}
